package initialize

import (
	"github.com/go-redis/redis"
)

// 安装包
// 1.go get -u github.com/go-redis/redis
// 2.本身不支持动态切换库
// 3.需要维护多个连接

var defaultRedisConnect *redis.Client
var library1RedisConnect *redis.Client

func RedisInit() {
	opt := &redis.Options{}
	host := Env("redis", "host").Value()
	port := Env("redis", "port").Value()
	password := Env("redis", "password").Value()
	db, _ := Env("redis", "db").Int()
	poolSize, _ := Env("redis", "poolSize").Int()
	minIdleConn, _ := Env("redis", "minIdleConn").Int()

	opt.Addr = host + ":" + port
	opt.Password = password
	opt.DB = db
	opt.PoolSize = poolSize
	opt.MinIdleConns = minIdleConn

	client := redis.NewClient(opt)
	_, err := client.Ping().Result()

	if err != nil {
		panic("连接redis失败：" + err.Error())
	}

	defaultRedisConnect = client

	host1 := Env("redis1", "host").Value()
	port1 := Env("redis1", "port").Value()
	password1 := Env("redis1", "password").Value()
	db1, _ := Env("redis1", "db").Int()
	poolSize1, _ := Env("redis1", "poolSize").Int()
	minIdleConn1, _ := Env("redis1", "minIdleConn").Int()

	opt1 := &redis.Options{}
	opt1.Addr = host1 + ":" + port1
	opt1.Password = password1
	opt1.DB = db1
	opt1.PoolSize = poolSize1
	opt1.MinIdleConns = minIdleConn1
	client1 := redis.NewClient(opt1)
	_, err = client1.Ping().Result()

	if err != nil {
		panic("连接redis 1号库失败：" + err.Error())
	}

	library1RedisConnect = client1

	return
}

func DefaultRedisConnect() *redis.Client {
	return defaultRedisConnect
}

func Library1RedisConnect() *redis.Client {
	return library1RedisConnect
}
